<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000">
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top">
  
  
  <title>用最容易的方式学会单链表（Python实现） | 这是宇宙之一粟的博客</title>
  <meta name="description" content="单链表与数组在本博客中，我们介绍单链表这种数据结构，链表结构为基于数组的序列提供了另一种选择（例如Python列表）。 基于数组的序列也会有如下缺点：  一个动态数组的长度可能超过实际存储数组元素所需的长度 在实时系统中对操作的摊销边界是不可接受的 在一个数组内部执行插入和删除操作的代价太高  基于数组的序列和链表都能够对其中的元素保持一定的顺序，但采用的方式截然不同。  数组是采用一整块的内存，">
<meta name="keywords" content="数据结构与算法">
<meta property="og:type" content="article">
<meta property="og:title" content="用最容易的方式学会单链表（Python实现）">
<meta property="og:url" content="http://yuzhou_1su.gitee.io/myblogs/2019/10/30/用最容易的方式学会单链表（Python实现）/index.html">
<meta property="og:site_name" content="小宇的博客">
<meta property="og:description" content="单链表与数组在本博客中，我们介绍单链表这种数据结构，链表结构为基于数组的序列提供了另一种选择（例如Python列表）。 基于数组的序列也会有如下缺点：  一个动态数组的长度可能超过实际存储数组元素所需的长度 在实时系统中对操作的摊销边界是不可接受的 在一个数组内部执行插入和删除操作的代价太高  基于数组的序列和链表都能够对其中的元素保持一定的顺序，但采用的方式截然不同。  数组是采用一整块的内存，">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/5283092-4be133207983b729.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/5283092-f95ae2fcd0bd500b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/5283092-1316aa86e2241ce7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/5283092-7a4af99bf6a04a3f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/5283092-c133743842e55965.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-10-30T13:23:12.231Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="用最容易的方式学会单链表（Python实现）">
<meta name="twitter:description" content="单链表与数组在本博客中，我们介绍单链表这种数据结构，链表结构为基于数组的序列提供了另一种选择（例如Python列表）。 基于数组的序列也会有如下缺点：  一个动态数组的长度可能超过实际存储数组元素所需的长度 在实时系统中对操作的摊销边界是不可接受的 在一个数组内部执行插入和删除操作的代价太高  基于数组的序列和链表都能够对其中的元素保持一定的顺序，但采用的方式截然不同。  数组是采用一整块的内存，">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/5283092-4be133207983b729.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
  <!-- Canonical links -->
  <link rel="canonical" href="http://yuzhou_1su.gitee.io/myblogs/2019/10/30/用最容易的方式学会单链表（Python实现）/index.html">
  
    <link rel="alternate" href="/atom.xml" title="小宇的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/myblogs/css/style.css">
  
  
  
  
</head>


<body class="main-center theme-black" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="http://yuzhou_1su.gitee.io/myblogs" target="_blank">
          <img class="img-circle img-rotate" src="/myblogs/images/yuzhou_1su.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">宇宙之一粟</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Python Developer &amp; 阅读爱好者</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Guangzhou, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索">
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech>
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav ">
        
        
        <li class="menu-item menu-item-home">
          <a href="/myblogs/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/myblogs/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/myblogs/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/yuzhouStayHungry" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="https://www.jianshu.com/u/93e9b14c6fbd" target="_blank" title="简书" data-toggle="tooltip" data-placement="top"><i class="icon icon-简书"></i></a></li>
        
        <li><a href="https://www.cnblogs.com/yuzhou-1su/" target="_blank" title="Blog" data-toggle="tooltip" data-placement="top"><i class="icon icon-Blog"></i></a></li>
        
        <li><a href="https://blog.csdn.net/yuzhou_1shu" target="_blank" title="CSDN" data-toggle="tooltip" data-placement="top"><i class="icon icon-CSDN"></i></a></li>
        
        <li><a href="/myblogs/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p> 欢迎一起探讨跟学习 </p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/myblogs/categories/技术/">技术</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/myblogs/categories/文学/">文学</a><span class="category-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/myblogs/tags/hexo配置/">hexo配置</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/myblogs/tags/数据结构与算法/">数据结构与算法</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/myblogs/tags/读书笔记/">读书笔记</a><span class="tag-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/myblogs/tags/hexo配置/" style="font-size: 13px;">hexo配置</a> <a href="/myblogs/tags/数据结构与算法/" style="font-size: 13.5px;">数据结构与算法</a> <a href="/myblogs/tags/读书笔记/" style="font-size: 14px;">读书笔记</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/myblogs/archives/2019/11/">十一月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/myblogs/archives/2019/10/">十月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/myblogs/archives/2019/07/">七月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/myblogs/archives/2019/06/">六月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/myblogs/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/myblogs/archives/2018/07/">七月 2018</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/myblogs/categories/技术/">技术</a>
              </p>
              <p class="item-title">
                <a href="/myblogs/2019/11/02/用最复杂的方式学会数组（Python实现动态数组）/" class="title">用最容易的方式学会单链表（Python实现）</a>
              </p>
              <p class="item-date">
                <time datetime="2019-11-02T02:26:58.362Z" itemprop="datePublished">2019-11-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/myblogs/categories/技术/">技术</a>
              </p>
              <p class="item-title">
                <a href="/myblogs/2019/10/30/用最容易的方式学会单链表（Python实现）/" class="title">用最容易的方式学会单链表（Python实现）</a>
              </p>
              <p class="item-date">
                <time datetime="2019-10-30T09:52:30.000Z" itemprop="datePublished">2019-10-30</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/myblogs/categories/文学/">文学</a>
              </p>
              <p class="item-title">
                <a href="/myblogs/2019/07/18/如何写作/" class="title">如何高效写作</a>
              </p>
              <p class="item-date">
                <time datetime="2019-07-18T09:52:30.000Z" itemprop="datePublished">2019-07-18</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/myblogs/categories/文学/">文学</a>
              </p>
              <p class="item-title">
                <a href="/myblogs/2019/06/30/《沟通圣经》/" class="title">读《沟通圣经》</a>
              </p>
              <p class="item-date">
                <time datetime="2019-06-30T12:52:30.000Z" itemprop="datePublished">2019-06-30</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/myblogs/2019/06/30/SQ3R阅读法/" class="title">SQ3R阅读法</a>
              </p>
              <p class="item-date">
                <time datetime="2019-06-30T12:52:30.000Z" itemprop="datePublished">2019-06-30</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <nav id="toc" class="article-toc">
      <h3 class="toc-title">文章目录</h3>
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#单链表与数组"><span class="toc-number">1.</span> <span class="toc-text">单链表与数组</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#链式结构"><span class="toc-number">1.1.</span> <span class="toc-text">链式结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Python中的引用"><span class="toc-number">1.2.</span> <span class="toc-text">Python中的引用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#节点定义与Python代码实现"><span class="toc-number">1.3.</span> <span class="toc-text">节点定义与Python代码实现</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#那么，什么是单链表"><span class="toc-number">1.4.</span> <span class="toc-text">那么，什么是单链表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#单链表有哪些操作"><span class="toc-number">1.5.</span> <span class="toc-text">单链表有哪些操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#增"><span class="toc-number">1.5.1.</span> <span class="toc-text">增</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#删"><span class="toc-number">1.5.2.</span> <span class="toc-text">删</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#改"><span class="toc-number">1.5.3.</span> <span class="toc-text">改</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查"><span class="toc-number">1.5.4.</span> <span class="toc-text">查</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#其他操作"><span class="toc-number">1.5.5.</span> <span class="toc-text">其他操作</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Python实现单链表的上述操作"><span class="toc-number">1.6.</span> <span class="toc-text">Python实现单链表的上述操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#输出的测试结果"><span class="toc-number">1.7.</span> <span class="toc-text">输出的测试结果</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#总结"><span class="toc-number">2.</span> <span class="toc-text">总结</span></a></li></ol>
    </nav>
  </div>
</aside>

<main class="main" role="main">
  <div class="content">
  <article id="post-用最容易的方式学会单链表（Python实现）" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      用最容易的方式学会单链表（Python实现）
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/myblogs/2019/10/30/用最容易的方式学会单链表（Python实现）/" class="article-date">
	  <time datetime="2019-10-30T09:52:30.000Z" itemprop="datePublished">2019-10-30</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/myblogs/categories/技术/">技术</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/myblogs/tags/数据结构与算法/">数据结构与算法</a>
  </span>


        

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/myblogs/2019/10/30/用最容易的方式学会单链表（Python实现）/#comments" class="article-comment-link">评论</a></span>
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <h1 id="单链表与数组"><a href="#单链表与数组" class="headerlink" title="单链表与数组"></a>单链表与数组</h1><p>在本博客中，我们介绍<strong>单链表</strong>这种数据结构，链表结构为基于数组的序列提供了另一种选择（例如Python列表）。</p>
<p>基于数组的序列也会有如下缺点：</p>
<ol>
<li>一个动态数组的长度可能超过实际存储数组元素所需的长度</li>
<li>在实时系统中对操作的摊销边界是不可接受的</li>
<li>在一个数组内部执行插入和删除操作的代价太高</li>
</ol>
<p>基于数组的序列和链表都能够对其中的元素保持一定的顺序，但采用的方式截然不同。</p>
<ul>
<li>数组是采用一整块的内存，能够为许多元素提供存储和引用。</li>
<li>链表则是用更为分散的结构，采用称为<strong>节点</strong>的轻量级对象，分配给每一个元素。每个节点维护一个指向它的元素的引用，并含一个或多个指向相邻节点的引用。</li>
</ul>
<h2 id="链式结构"><a href="#链式结构" class="headerlink" title="链式结构"></a>链式结构</h2><p>什么是线性表的链式存储，即采用一组任意的存储单元存放线性表的元素，这组存储元素可以是连续的，也可以是不连续的。连续的我们当然好理解，那如果不连续呢？就可以通过一条链来连接，什么是链？最直观的感受如下图：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/5283092-4be133207983b729.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="chains_1251px_1205993_easyicon.net.png"></p>
<p>我们知道，C语言中有指针，指针通过地址来找到它的目标。如此说来，一个节点不仅仅有它的元素，还需要有一个它的下一个元素的地址。这两部分构成的存储结构称为节点（node），即节点包含两个域：数据域和指针域，结构的结构图如下：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/5283092-f95ae2fcd0bd500b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="节点.png"></p>
<h2 id="Python中的引用"><a href="#Python中的引用" class="headerlink" title="Python中的引用"></a>Python中的引用</h2><p>那么，这里需要指针和地址，我们在学习基础的时候没听说Python有C或C++中的指针啊，Python中指针是什么？我们先把这个概念放一放，一提到指针可能初学C和C++的人都害怕（本人也害怕），先来理解一下Python里面变量的本质。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>id(a)</span><br><span class="line"><span class="number">4343720720</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>id(b)</span><br><span class="line"><span class="number">4343720720</span></span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>

<p>我们能看到id(a) == id(b)，为什么a和b的ID是一样的呢？我们来看一下这个图：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/5283092-1316aa86e2241ce7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="id.png"></p>
<p>我们利用上图来打一个比喻，可能不是很准确方便我们进行理解。如果计算机中当成是一栋房屋，那么内存地址（4343720720）就应该是其中的一个房子，这个房子可以存储数据（100，也可以存10），而房名就是变量名（a、b）。房子被小a买走了（利用起来），小a就指向这个房子，同理，小b也指向这个房子。房子里存储的数据都是100，所以虽然a和b的名字不同，但他们住同一房子。</p>
<p>我们再来看一下下面这个代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a, b = <span class="number">10</span>, <span class="number">20</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>id(a)</span><br><span class="line"><span class="number">4343717840</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>id(b)</span><br><span class="line"><span class="number">4343718160</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a, b = b, a</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>id(a)</span><br><span class="line"><span class="number">4343718160</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>id(b)</span><br><span class="line"><span class="number">4343717840</span></span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>

<p><img src="https://upload-images.jianshu.io/upload_images/5283092-7a4af99bf6a04a3f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="2.png"></p>
<p>是不是也非常好理解了，小a和小b买了不同的房子（4343717840和4343718160），存着不同的数据（a=10, b=20）。最后他们交换了房子，同时交换了房里的数据。</p>
<p>变量本身就存储的一个地址，交换他们的值就是把自己的指向更改。Python中没有指针，所以实际编程一般用引用来代替。这里对Python引用的介绍不是很详细，如果读者还是不明白，可以通过其他的资料进行深入了解。</p>
<h2 id="节点定义与Python代码实现"><a href="#节点定义与Python代码实现" class="headerlink" title="节点定义与Python代码实现"></a>节点定义与Python代码实现</h2><p>节点，用于构建单链表的一部分，有两个成员：元素成员、指针域成员。</p>
<p>元素成员：引用一个任意的对象，该对象是序列中的一个元素，下图中的a1、a2、…、an</p>
<p>指针域成员：指向单链表的后继节点，如果没有后继节点，则为空</p>
<p><img src="https://upload-images.jianshu.io/upload_images/5283092-c133743842e55965.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="timg.jpeg"></p>
<p>熟悉完链式结构，我们就能很好的写出节点的Python代码了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="string">"""声明节点"""</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, element)</span>:</span></span><br><span class="line">        self.element = element  <span class="comment"># 给定一个元素</span></span><br><span class="line">        self.next = <span class="literal">None</span>  <span class="comment"># 初始设置下一节点为空</span></span><br></pre></td></tr></table></figure>

<h2 id="那么，什么是单链表"><a href="#那么，什么是单链表" class="headerlink" title="那么，什么是单链表"></a>那么，什么是单链表</h2><p><strong>单链表</strong> 最简单的形式就是由多个节点的集合共同构成一个线性序列。每个节点存储一个对象的引用，这个引用指向序列中的一个元素，即存储指向列表的下一个节点。</p>
<blockquote>
<p>单链表是一种链式存取的数据结构，用一组地址任意的<a href="https://baike.baidu.com/item/存储单元/8727749" target="_blank" rel="noopener">存储单元</a>存放线性表中的<a href="https://baike.baidu.com/item/数据元素/715313" target="_blank" rel="noopener">数据元素</a>。链表中的数据是以结点来表示的，每个结点的构成：元素(<a href="https://baike.baidu.com/item/数据元素/715313" target="_blank" rel="noopener">数据元素</a>的映象) + <a href="https://baike.baidu.com/item/指针/2878304" target="_blank" rel="noopener">指针</a>(指示后继元素存储位置)，元素就是存储数据的存储单元，指针就是连接每个结点的地址数据。</p>
</blockquote>
<p>其实，上面的术语用生活中的大白话来解释，就是我们现在有三个人——我、你、他。当我用手指指向你（注意：因为是单链表，所以你不能指向我），你用手指指向他，这样就形成了一个单链表。手指就是一个引用，而“我、你、他”就是序列中的元素。“我-&gt;你-&gt;他”方式就是一个简单单链表，不知道你理解了没有？ </p>
<ul>
<li><p>头结点：链表的第一个节点</p>
</li>
<li><p>尾节点：链表的最后一个节点</p>
<p>从头节点开始，通过每个节点的<strong>“next”</strong>引用，可以从一个节点移动到另一个节点，从而最终到达列表的尾节点。</p>
<p>若当前节点的<strong>“next”</strong>引用指向空时，我们可以确定该节点为尾节点。这一过程，我们通过叫做遍历链表。</p>
</li>
</ul>
<h2 id="单链表有哪些操作"><a href="#单链表有哪些操作" class="headerlink" title="单链表有哪些操作"></a>单链表有哪些操作</h2><p>链表的操作并不是很难，只要明白节点的结构：数据域element和指针域next。而各种操作其实就是对指针的操作，不论是增删改查，都是先找指针，再取元素。具体有哪些基础操作是我实现的呢？如下（当然，还有更多的操作可能使我没想到的，希望你能在评论中提出来。）</p>
<h3 id="增"><a href="#增" class="headerlink" title="增"></a>增</h3><ul>
<li>头插法</li>
<li>尾插法</li>
<li>指定位置将元素插入</li>
</ul>
<h3 id="删"><a href="#删" class="headerlink" title="删"></a>删</h3><ul>
<li>删除头结点</li>
<li>删除尾节点</li>
<li>删除指定元素</li>
</ul>
<h3 id="改"><a href="#改" class="headerlink" title="改"></a>改</h3><ul>
<li>修改指定位置上的元素</li>
</ul>
<h3 id="查"><a href="#查" class="headerlink" title="查"></a>查</h3><ul>
<li><p>遍历整个单链表</p>
</li>
<li><p>查询指定元素是否存在</p>
</li>
</ul>
<h3 id="其他操作"><a href="#其他操作" class="headerlink" title="其他操作"></a>其他操作</h3><ul>
<li>链表判空</li>
<li>求链表长度</li>
<li>反转整个链表（面试高频考点）</li>
</ul>
<h2 id="Python实现单链表的上述操作"><a href="#Python实现单链表的上述操作" class="headerlink" title="Python实现单链表的上述操作"></a>Python实现单链表的上述操作</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time      : 2019-10-30 15:50</span></span><br><span class="line"><span class="comment"># @Author    : yuzhou_1su</span></span><br><span class="line"><span class="comment"># @ContactMe : https://blog.csdn.net/yuzhou_1shu</span></span><br><span class="line"><span class="comment"># @File      : singly_linked_list.py</span></span><br><span class="line"><span class="comment"># @Software  : PyCharm</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="string">"""声明节点"""</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, element)</span>:</span></span><br><span class="line">        self.element = element  <span class="comment"># 给定一个元素</span></span><br><span class="line">        self.next = <span class="literal">None</span>  <span class="comment"># 初始设置下一节点为空</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Singly_linked_list</span>:</span></span><br><span class="line">    <span class="string">"""Python实现单链表"""</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self)</span>:</span></span><br><span class="line">        self.__head = <span class="literal">None</span>  <span class="comment"># head设置为私有属性，禁止外部访问</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">is_empty</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="string">"""判断链表是否为空"""</span></span><br><span class="line">        <span class="keyword">return</span> self.__head <span class="keyword">is</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">length</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="string">"""返回链表长度"""</span></span><br><span class="line">        cur = self.__head  <span class="comment"># cur游标，用来移动遍历节点</span></span><br><span class="line">        count = <span class="number">0</span>  <span class="comment"># count记录节点数量</span></span><br><span class="line">        <span class="keyword">while</span> cur <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">            count += <span class="number">1</span></span><br><span class="line">            cur = cur.next</span><br><span class="line">        <span class="keyword">return</span> count</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">travel_list</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="string">"""遍历整个链表，打印每个节点的数据"""</span></span><br><span class="line">        cur = self.__head</span><br><span class="line">        <span class="keyword">while</span> cur <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">            print(cur.element, end=<span class="string">" "</span>)</span><br><span class="line">            cur = cur.next</span><br><span class="line">        print(<span class="string">"\n"</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">insert_head</span><span class="params">(self, element)</span>:</span></span><br><span class="line">        <span class="string">"""头插法：在单链表头部插入一个节点"""</span></span><br><span class="line">        newest = Node(element)  <span class="comment"># 创建一个新节点</span></span><br><span class="line">        <span class="keyword">if</span> self.__head <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:  <span class="comment"># 如果初始不为空，就将新节点的"next"指针指向head</span></span><br><span class="line">            newest.next = self.__head</span><br><span class="line">        self.__head = newest  <span class="comment"># 把新节点设置为head</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">insert_tail</span><span class="params">(self, element)</span>:</span></span><br><span class="line">        <span class="string">"""尾插法：在单链表尾部增加一个节点"""</span></span><br><span class="line">        <span class="keyword">if</span> self.__head <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            self.insert_head(element)  <span class="comment"># 如果这是第一个节点，调用insert_head函数</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            cur = self.__head</span><br><span class="line">            <span class="keyword">while</span> cur.next <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:  <span class="comment"># 遍历到最后一个节点</span></span><br><span class="line">                cur = cur.next</span><br><span class="line">            cur.next = Node(element)  <span class="comment"># 创建新节点并连接到最后</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">insert</span><span class="params">(self, pos, element)</span>:</span></span><br><span class="line">        <span class="string">"""指定位置插入元素"""</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 如果位置在0或者之前，调用头插法</span></span><br><span class="line">        <span class="keyword">if</span> pos &lt; <span class="number">0</span>:</span><br><span class="line">            self.insert_head(element)</span><br><span class="line">        <span class="comment"># 如果位置在原链表长度之后，调用尾插法</span></span><br><span class="line">        <span class="keyword">elif</span> pos &gt; self.length() - <span class="number">1</span>:</span><br><span class="line">            self.insert_tail(element)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            cur = self.__head</span><br><span class="line">            count = <span class="number">0</span></span><br><span class="line">            <span class="keyword">while</span> count &lt; pos - <span class="number">1</span>:</span><br><span class="line">                count += <span class="number">1</span></span><br><span class="line">                cur = cur.next</span><br><span class="line">            newest = Node(element)</span><br><span class="line">            newest.next = cur.next</span><br><span class="line">            cur.next = newest</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">delete_head</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="string">"""删除头结点"""</span></span><br><span class="line">        cur = self.__head</span><br><span class="line">        <span class="keyword">if</span> self.__head != Node:</span><br><span class="line">            self.__head = self.__head.next</span><br><span class="line">            cur.next = <span class="literal">None</span></span><br><span class="line">        <span class="keyword">return</span> cur</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">delete_tail</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="string">"""删除尾节点"""</span></span><br><span class="line">        cur = self.__head</span><br><span class="line">        <span class="keyword">if</span> self.__head <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">            <span class="keyword">if</span> self.__head.next <span class="keyword">is</span> <span class="literal">None</span>:  <span class="comment"># 如果头结点是唯一的节点</span></span><br><span class="line">                self.__head = <span class="literal">None</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="keyword">while</span> cur.next.next <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">                    cur = cur.next</span><br><span class="line">                cur.next, cur = (<span class="literal">None</span>, cur.next)</span><br><span class="line">        <span class="keyword">return</span> cur</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">remove</span><span class="params">(self, element)</span>:</span></span><br><span class="line">        <span class="string">"""删除指定元素"""</span></span><br><span class="line">        cur, prev = self.__head, <span class="literal">None</span></span><br><span class="line">        <span class="keyword">while</span> cur <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">            <span class="keyword">if</span> cur.element == element:</span><br><span class="line">                <span class="keyword">if</span> cur == self.__head:  <span class="comment"># 如果该节点是头结点</span></span><br><span class="line">                    self.__head = cur.next</span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    prev.next = cur.next</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                prev, cur = cur, cur.next</span><br><span class="line">        <span class="keyword">return</span> cur.element</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">modify</span><span class="params">(self, pos, element)</span>:</span></span><br><span class="line">        <span class="string">"""修改指定位置的元素"""</span></span><br><span class="line">        cur = self.__head</span><br><span class="line">        <span class="keyword">if</span> pos &lt; <span class="number">0</span> <span class="keyword">or</span> pos &gt; self.length():</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> range(pos - <span class="number">1</span>):</span><br><span class="line">            cur = cur.next</span><br><span class="line">        cur.element = element</span><br><span class="line">        <span class="keyword">return</span> cur</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">search</span><span class="params">(self, element)</span>:</span></span><br><span class="line">        <span class="string">"""查找节点是否存在"""</span></span><br><span class="line">        cur = self.__head</span><br><span class="line">        <span class="keyword">while</span> cur:</span><br><span class="line">            <span class="keyword">if</span> cur.element == element:</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                cur = cur.next</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">reverse_list</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="string">"""反转整个链表"""</span></span><br><span class="line">        cur, prev = self.__head, <span class="literal">None</span></span><br><span class="line">        <span class="keyword">while</span> cur:</span><br><span class="line">            cur.next, prev, cur = prev, cur, cur.next</span><br><span class="line">        self.__head = prev</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    List1 = Singly_linked_list()</span><br><span class="line">    print(<span class="string">"链表是否为空"</span>, List1.is_empty())</span><br><span class="line"></span><br><span class="line">    List1.insert_head(<span class="number">1</span>)</span><br><span class="line">    List1.insert_head(<span class="number">2</span>)</span><br><span class="line">    List1.insert_tail(<span class="number">3</span>)</span><br><span class="line">    List1.insert_tail(<span class="number">4</span>)</span><br><span class="line">    List1.insert_tail(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">    length_of_list1 = List1.length()</span><br><span class="line">    print(<span class="string">"插入节点后，List1 的长度为："</span>, length_of_list1)</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"遍历并打印整个链表: "</span>)</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"反转整个链表: "</span>)</span><br><span class="line">    List1.reverse_list()</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"删除头节点: "</span>)</span><br><span class="line">    List1.delete_head()</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"删除尾节点: "</span>)</span><br><span class="line">    List1.delete_tail()</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"在第二个位置插入5: "</span>)</span><br><span class="line">    List1.insert(<span class="number">1</span>, <span class="number">5</span>)</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"在第-1个位置插入100："</span>)</span><br><span class="line">    List1.insert(<span class="number">-1</span>, <span class="number">100</span>)</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"在第100个位置插入2："</span>)</span><br><span class="line">    List1.insert(<span class="number">100</span>, <span class="number">2</span>)</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"删除元素5："</span>)</span><br><span class="line">    print(List1.remove(<span class="number">5</span>))</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"修改第5个位置的元素为7: "</span>)</span><br><span class="line">    List1.modify(<span class="number">5</span>, <span class="number">7</span>)</span><br><span class="line">    List1.travel_list()</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"查找元素1:"</span>)</span><br><span class="line">    print(List1.search(<span class="number">1</span>))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>

<h2 id="输出的测试结果"><a href="#输出的测试结果" class="headerlink" title="输出的测试结果"></a>输出的测试结果</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">链表是否为空 True</span><br><span class="line">插入节点后，List1 的长度为： 5</span><br><span class="line">遍历并打印整个链表: </span><br><span class="line">2 1 3 4 5 </span><br><span class="line"></span><br><span class="line">反转整个链表: </span><br><span class="line">5 4 3 1 2 </span><br><span class="line"></span><br><span class="line">删除头节点: </span><br><span class="line">4 3 1 2 </span><br><span class="line"></span><br><span class="line">删除尾节点: </span><br><span class="line">4 3 1 </span><br><span class="line"></span><br><span class="line">在第二个位置插入5: </span><br><span class="line">4 5 3 1 </span><br><span class="line"></span><br><span class="line">在第-1个位置插入100：</span><br><span class="line">100 4 5 3 1 </span><br><span class="line"></span><br><span class="line">在第100个位置插入2：</span><br><span class="line">100 4 5 3 1 2 </span><br><span class="line"></span><br><span class="line">删除元素5：</span><br><span class="line">5</span><br><span class="line">100 4 3 1 2 </span><br><span class="line"></span><br><span class="line">修改第5个位置的元素为7: </span><br><span class="line">100 4 3 1 7 </span><br><span class="line"></span><br><span class="line">查找元素1:</span><br><span class="line">True</span><br></pre></td></tr></table></figure>

<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>在我们对这些基础操作熟练之后，我推荐的学习方法就是对网上（比如LeetCode）上与单链表相关的习题进行练习。具体有哪些好的习题呢？等后面写博客找一些经典的题并把思路写出来，如果你找到了好的题欢迎分享给我，一起学习探讨。</p>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://yuzhou_1su.gitee.io/myblogs/2019/10/30/用最容易的方式学会单链表（Python实现）/" title="用最容易的方式学会单链表（Python实现）" target="_blank" rel="external">http://yuzhou_1su.gitee.io/myblogs/2019/10/30/用最容易的方式学会单链表（Python实现）/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="http://yuzhou_1su.gitee.io/myblogs" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/myblogs/images/yuzhou_1su.jpg" class="img-rounded w-full" alt>
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="http://yuzhou_1su.gitee.io/myblogs" target="_blank"><span class="text-dark">宇宙之一粟</span><small class="ml-1x">Python Developer &amp; 阅读爱好者</small></a></h3>
        <div>觉宇宙之无穷，渺沧海之一粟。文理兼修但不都精通的码农</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/myblogs/2019/11/02/用最复杂的方式学会数组（Python实现动态数组）/" title="用最容易的方式学会单链表（Python实现）"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/myblogs/2019/07/18/如何写作/" title="如何高效写作"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
    <li class="toggle-toc">
      <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false" title="文章目录" role="button">
        <span>[&nbsp;</span><span>文章目录</span>
        <i class="text-collapsed icon icon-anchor"></i>
        <i class="text-in icon icon-close"></i>
        <span>]</span>
      </a>
    </li>
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>赏</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>感谢您的支持，我会继续努力的!</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="/myblogs/images/donate/alipayimg.png" alt="扫码支持" title="扫一扫">
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开支付宝扫一扫，即可进行扫码打赏哦</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="/myblogs/images/donate/wechatpayimg.png" alt="扫码支持" title="扫一扫">
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开微信扫一扫，即可进行扫码打赏哦</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> 支付宝</a>
              </li>
              <li role="presentation" class>
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> 微信支付</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/yuzhouStayHungry" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="https://www.jianshu.com/u/93e9b14c6fbd" target="_blank" title="简书" data-toggle="tooltip" data-placement="top"><i class="icon icon-简书"></i></a></li>
        
        <li><a href="https://www.cnblogs.com/yuzhou-1su/" target="_blank" title="Blog" data-toggle="tooltip" data-placement="top"><i class="icon icon-Blog"></i></a></li>
        
        <li><a href="https://blog.csdn.net/yuzhou_1shu" target="_blank" title="CSDN" data-toggle="tooltip" data-placement="top"><i class="icon icon-CSDN"></i></a></li>
        
        <li><a href="/myblogs/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/myblogs/js/plugin.min.js"></script>
<script src="/myblogs/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/myblogs/',
        CONTENT_URL: '/myblogs/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/myblogs/js/insight.js"></script>





   




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: '',
    appKey: '',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: false
  });
  </script>

     







</body>
</html>